//
//  MineManagerHomeViewController.m
//  PYWGetColor
//
//  Created by 杜文杰 on 2018/6/15.
//  Copyright © 2018年 dwj. All rights reserved.
//

#import "MineManagerHomeViewController.h"
#import "MineManagerHomeTableViewCell.h"
#import "YXSerachServerModel.h"
#import "DetailMakeSureViewController.h"
#import "WJChatViewController.h"

@interface MineManagerHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *mainTableView; // 主框架
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *footerView;

@property (nonatomic, strong) NSMutableArray *datamuArray;
@property (nonatomic, strong) YXSerachServerModel *serachServerModel; //数据源.item
@end

@implementation MineManagerHomeViewController
- (void)myMnangerDataList {
    [self.datamuArray removeAllObjects];
    [[AFAppDotNetAPIClient sharedClient] POST:@"interface.php?m=mycollect" parameters:@{@"uid":[UserModel sharedInstanced].userId,@"token":[UserModel sharedInstanced].token,@"lat":[UserModel sharedInstanced].latitude,@"lnt":[UserModel sharedInstanced].longitude} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"error_code"] isEqualToString:@"0"]) {
            self.serachServerModel = [YXSerachServerModel yy_modelWithDictionary:responseObject[@"data"]];
            [self.datamuArray addObjectsFromArray:self.serachServerModel.item];
            [self.mainTableView reloadData];
        }else{
            ShowMessage(@"获取数据失败");
        }
        [self.mainTableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.mainTableView.mj_header endRefreshing];
        [self showError:error];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"我的管家";
    [self.view addSubview:self.mainTableView];
    [self myMnangerDataList];
    
    //1 使用框架UI样式，直接调用
    self.mainTableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"kongBai"
                                                            titleStr:@"暂无数据，点击重新加载"
                                                           detailStr:@""];
    //emptyView内容上的点击事件监听
    __weak typeof(self)weakSelf = self;
    [self.mainTableView.ly_emptyView setTapContentViewBlock:^(){
        [weakSelf.mainTableView.mj_header beginRefreshing];
    }];
    
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datamuArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineManagerHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MineManagerHomeTableViewCell class])];
    ServerModellList *model = (ServerModellList *)self.datamuArray[indexPath.section];
    cell.serverModellList = model;
    cell.telephoneBtnBlock = ^{
        [self callPhone:model.mobile];
    };
    cell.talkAboutBtnBlock = ^{
        //环信ID:@"8001"
        //聊天类型:EMConversationTypeChat
        WJChatViewController *chatController = [[WJChatViewController alloc] initWithConversationChatter:model.mobile conversationType:EMConversationTypeChat];
        [self.navigationController pushViewController:chatController animated:YES];
    };
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailMakeSureViewController *VC = [[DetailMakeSureViewController alloc]init];
    VC.serverModellList = (ServerModellList *)self.datamuArray[indexPath.section];
    VC.typeStrXq = @"2";
    [self.navigationController pushViewController:VC animated:NO];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10 / HEIGHT_6S_SCALE;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = getColor(dividerColor);
    
    return bgView;
}
#pragma mark - init
- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - SafeAreaTopHeight) style:UITableViewStyleGrouped];
        _mainTableView.backgroundColor = getColor(whiteColor);
        _mainTableView.tableHeaderView = self.headerView;
        _mainTableView.tableFooterView = self.footerView;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.showsHorizontalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        // 动态行高
        _mainTableView.rowHeight = UITableViewAutomaticDimension;
        // 预估行高
        _mainTableView.estimatedRowHeight = 80;
        [_mainTableView registerClass:[MineManagerHomeTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MineManagerHomeTableViewCell class])];
        //刷新、加载
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self myMnangerDataList];
        }];
    }
    return _mainTableView;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
        _headerView.backgroundColor = [UIColor grayColor];
    }
    return _headerView;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}
- (NSMutableArray *)datamuArray
{
    if (!_datamuArray) {
        _datamuArray = @[].mutableCopy;
    }
    return _datamuArray;
}
@end
